﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.System;
using WMS.FC.Domain.Service.Tray.Interface;

namespace WMS.FC.Domain.Service.Tray
{
    public class CheckTrayExistService : ServiceBase, ICheckTrayExistService
    {
        protected readonly ITrayRepository trayRepository;
        protected readonly ICheckTrayBarcodeService checkTrayBarcodeService;

        public CheckTrayExistService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ICheckTrayBarcodeService checkTrayBarcodeService,
            ITrayRepository trayRepository)
            : base(logger, options, mapper)
        {
            this.trayRepository = trayRepository;
            this.checkTrayBarcodeService = checkTrayBarcodeService;
        }

        public async Task<ActionResult<TrayPO>> CheckTrayExist(string trayBarcode)
        {
            var result = new ActionResult<TrayPO>("校验托盘是否登录");
            try
            {
                var checkTrayBarcodeResult = await checkTrayBarcodeService.CheckTrayBarcode(trayBarcode);
                if (!checkTrayBarcodeResult.IsSuccess)
                    return result.Fail(checkTrayBarcodeResult);

                var trayPO = await trayRepository.GetTrayByBarcode(trayBarcode);
                if (trayPO == null)
                    return result.PartSuccess("托盘未登录");

                result.Data = trayPO;
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
